package com.ieasy.module.system.web.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ieasy.basic.model.DataGrid;
import com.ieasy.basic.model.Msg;
import com.ieasy.basic.util.ChangeCharset;
import com.ieasy.basic.util.cons.Const;
import com.ieasy.basic.util.date.DateUtils;
import com.ieasy.basic.util.poi.excel.ExcelUtil;
import com.ieasy.module.common.dto.PersonInfoExcelDto;
import com.ieasy.module.common.web.action.BaseController;
import com.ieasy.module.system.service.IPersonInfoService;
import com.ieasy.module.system.web.form.PersonInfoForm;


@Controller
@RequestMapping("/admin/system/personinfo")
public class PersonInfoAction extends BaseController {
	
	@Inject
	private IPersonInfoService personInfoService ;
	
	
	@RequestMapping("/personinfo_main_UI.do")
	public String personinfo_main_UI() {
		return Const.SYSTEM + "personinfo_main_UI" ;
	}
	
	@RequestMapping("/personinfo_form_UI.do")
	public String person_form_UI(PersonInfoForm form, Model model) throws Exception {
		if(null != form.getId() && !"".equals(form.getId())) {
			model.addAttribute("id", form.getId()) ;
		}
		return Const.SYSTEM + "personinfo_form_UI" ;
	}
	
	
	
	
	
	@RequestMapping("/doNotNeedAuth_add.do")
	public @ResponseBody Msg add(PersonInfoForm form) throws Exception {
		return this.personInfoService.add(form) ;
	}
	
	@RequestMapping("/doNotNeedAuth_addOrigin.do")
	public @ResponseBody Msg doNotNeedAuth_addOrigin(PersonInfoForm form) throws Exception {
		return this.personInfoService.addOriginPerson(form) ;
	}
	
	@RequestMapping("/doNotNeedAuth_delete.do")
	public @ResponseBody Msg delete(PersonInfoForm form) throws Exception {
		return this.personInfoService.delete(form) ;
	}
	
	@RequestMapping("/doNotNeedAuth_update.do")
	public @ResponseBody Msg update(PersonInfoForm form) throws Exception {
		return this.personInfoService.update(form) ;
	}
	
	@RequestMapping("/get.do")
	public @ResponseBody PersonInfoForm get(PersonInfoForm form) throws Exception {
		return this.personInfoService.get(form) ;
	}
	
	@RequestMapping("/doNotNeedAuth_datagrid.do")
	public @ResponseBody DataGrid doNotNeedAuth_datagrid(PersonInfoForm form) {
		DataGrid datagrid= null;
		try {
			datagrid = this.personInfoService.datagrid(form);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return datagrid ;
	}
	
	@RequestMapping("/myPersoninfo_form_UI.do")
	public String myPersoninfo_form_UI(PersonInfoForm form, Model model) {
		if(null != form.getNum() && !"".equals(form.getNum())) {
			model.addAttribute("num", form.getNum()) ;
		}
		return Const.SYSTEM + "my_personinfo_form_UI" ;
	}
	@RequestMapping("/doNotNeedAuth_updateMyInfo.do")
	public @ResponseBody Msg doNotNeedAuth_updateMyInfo(PersonInfoForm form) throws Exception {
		return this.personInfoService.update(form) ;
	}
	
	
	@RequestMapping("/doNotNeedAuth_exportPersonInfoExcelData.do")
	public void exportPersonInfoExcelData(HttpServletResponse resp, PersonInfoForm form){
		try {
			resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") ;
			resp.setHeader("Content-disposition", "attachment;filename="+ChangeCharset.toISO_8859_1("赴日人员候补信息")+".xlsx");
			
			List<PersonInfoExcelDto> objs = this.personInfoService.exportPersonInfoData(form) ;
			
			Map<String, String> datas = new HashMap<String, String>() ;
			datas.put("title", "赴日人员候补信息") ;
			datas.put("total", String.valueOf(objs.size())) ;
			datas.put("date", DateUtils.formatYYYYMMDD(new Date())) ;
			ExcelUtil.getInstance().exportObj2ExcelByTemplate(datas, "resources/excel/template/emp_info.xlsx", resp.getOutputStream(), objs, PersonInfoExcelDto.class, true) ;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

